* Bartik regressions

clear 
use "${output_stata}\bartik_regression.dta"

eststo clear 
label variable HC_bartik_s_brut "HC Bartik$\_{\textit{g,n,03-11}}$"
label variable HC_distance_init "HC Distance$\_{\textit{g,n,03}}$"
label variable HC_bartik_mean "HC Bartik (simple mean)"
label variable share_N "Exposure to merger waves$\_{\textit{g,n,03-11}}$"

* Creating dummy for IT workers
gen dummy_info_t0 = info_t0 > 0 & info_t0 < . // Create a dummy variable for presence of IT workers
label variable info_t0 "Share IT workers$\_{\textit{g,03}}$"
label variable dummy_info_t0 "1(IT workers)$\_{\textit{g,03}}$"

* Merging additional data on IT share
merge m:1 code_entry_num using "${input_stata}d_share_it_s_brut.dta", nogen keep(3)

reghdfe buy HC_bartik_s_brut HC_bartik_nombre HC_bartik_nbheur    /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if share > 0.01 & share < . & year_t0 <= 2003, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
gen sample = e(sample) // Generate sample variable based on regression sample

********************************************************************************
* Descriptive statistics

eststo clear
eststo summa: quietly estpost summ /// 
HC_bartik_s_brut /// 
if e(sample), d 

esttab summa, ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") /// 
nonumber label fragment

esttab summa using "${export}\stat_summ_hc_bartik.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace

eststo clear
eststo summa: quietly estpost summ ///
HC_bartik_mean share_N ///
if sample, d 

esttab summa using "${export}\stat_summ_hc_bartik_supp.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace

********************************************************************************

* Creating a dummy variable and dividing into tertiles
local varlist d_share_it
foreach x of local varlist {
    egen q_`x'b = xtile(`x') if e(sample), nq(2) // Create tertiles for the variable
    drop if q_`x'b == . // Drop missing values
}

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_2
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
estimates store reg_3
estadd local orig_dest "No"
estadd local ctrl "Yes"
estadd local year "No"
estadd local orig_dest_year "Yes"

reghdfe buy HC_bartik_nombre     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_4
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_nbheur     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_5
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample & q_d_share_itb == 1, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_6
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample & q_d_share_itb == 2, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_7
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut HC_distance_init     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_8
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 dummy_info_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_9
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_mean     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_10
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"

reghdfe buy HC_bartik_s_brut     /*
*/ diversity_t0 size_t0 immo_eff_t0 tresact_eff_t0 vaj_eff_t0 sal_eff_t0 share_N if sample, absorb(orig_dest year) cluster(apgr_1_num code_entry_num) 
estimates store reg_11
estadd local orig_dest "Yes"
estadd local ctrl "Yes"
estadd local year "Yes"
estadd local orig_dest_year "No"


eststo clear
eststo summa: quietly estpost summ ///
dummy_info_t0  ///
if sample, d
 
esttab summa, ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") /// 
nonumber label fragment

esttab summa using "${export}\stat_summ_into_t0.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace

esttab reg_2 reg_3 reg_8 reg_6 reg_7 reg_9 reg_11 reg_10 using "${export}\bartik.tex", /*
*/ keep(HC_bartik* HC_distance_init dummy_info_t0 share_N) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /* 
*/ alignment(D{.}{.}{-1})  /*     
*/ title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/ collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest year orig_dest_year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin $\times$ Entry FE"' `"Year FE"' `"Sector of Origin $\times$ Entry $\times$ Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" /*
*/	"\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"))